package com.chy.blog_server.modular.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chy.blog_server.core.util.UploadFileUtil;
import com.chy.blog_server.core.util.UploadURLUtil;
import com.chy.blog_server.modular.web.entity.BoIndexImage;
import com.chy.blog_server.modular.web.mapper.BoIndexImageMapper;
import com.chy.blog_server.modular.web.service.IBoIndexImageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 描述：首页照片展示表 service业务层
 * 作者：chenhuiyan
 * 时间：2020/03/08 12:03
 */
@Service
@Transactional(readOnly = true)
public class BoIndexImageServiceImpl extends ServiceImpl<BoIndexImageMapper, BoIndexImage> implements IBoIndexImageService {

    private final static Logger logger = LoggerFactory.getLogger(BoIndexImageServiceImpl.class);

    /**
     * 查询列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> list(Map<String, Object> params){
        return baseMapper.list(params);
    }

    /**
     * 查询总数
     * @param params
     * @return
     */
    public int getCount(Map<String, Object> params){
        return baseMapper.getCount(params);
    }


    /**
     * 根据id查询详情
     * @param id
     * @return
     */
    public Map<String, Object> getInfoById(String id){
        return baseMapper.getInfoById(id);
    }

    /**
     * 查询已删除的文章列表
     * @param params
     * @return
     */
    public List<Map<String, Object>> recycleList(Map<String, Object> params){
        return baseMapper.recycleList(params);
    }

    /**
     * 彻底删除（删除原图和缩略图）
     * @param id
     * @return
     */
    @Transactional(readOnly = false)
    public int thoroughlyDelete(String id){
        //查询照片记录
        BoIndexImage boIndexImage = this.getById(id);

        //删除原图和缩略图
        boolean flag = true;
        String path = UploadURLUtil.INDEX_IMAGE; //路径

        String[] urlArr = new String[2];
        urlArr[0] = path + boIndexImage.getUrl();
        urlArr[1] = path + boIndexImage.getThumUrl();

        flag = UploadFileUtil.deleteMoreFile(urlArr);

        logger.info(">>>>>>>>>>>>删除首页照片成功<<<<<<<<<<<<<<<<");

        //删除图片成功则删表记录
        if(flag){
            int i = baseMapper.thoroughlyDelete(id);
            logger.info(">>>>>>>>>>>>删除首页照片表记录成功<<<<<<<<<<<<<<<<");
            return i;

        }else{
            return 0;
        }
    }

}
